﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Calendar.cs" company="HelloWorld">
//   BY王悉奥
// </copyright>
// <summary>
//   Defines the UserControl1 type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Control
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Windows.Forms;

    using WindowsFormsApp2.Service;

    /// <summary>
    /// The user control 1.
    /// </summary>
    public partial class Calendar : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Calendar"/> class.
        /// </summary>
        public Calendar()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// The calendar_ mouse down.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Cdr_MouseDown(object sender, MouseEventArgs e)
        {
            var monthCalendarSender = (MonthCalendar)sender;
            var hitTestInfo = monthCalendarSender.HitTest(e.Location);
            if (hitTestInfo.HitArea != MonthCalendar.HitArea.Date)
            {
                return;
            }

            var lists = DeadlineService.SelectByTime(this.cdr.SelectionStart.Date);
            this.dgv_ddl.Rows.Clear();
            foreach (var list in lists)
            {
                object[] listObjects = { list.DeadlineTime, list.Information, list.Course };
                this.dgv_ddl.Rows.Add(listObjects);
            }
        }

        /// <summary>
        /// The user control 1_ load.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1126:PrefixCallsCorrectly", Justification = "Reviewed. Suppression is OK here.")]
        private void UserControl1_Load(object sender, EventArgs e)
        {
            var list = DeadlineService.SelectAllDeadline();
            const string Pattern = "yyyy-MM-dd";
            var closestTime = list[0];
            var isOlder = false;
            foreach (var date in list)
            {
                DateTime.TryParseExact(date, Pattern, null, DateTimeStyles.None, out var dateTime);
                DateTime.TryParseExact(closestTime, Pattern, null, DateTimeStyles.None, out var closestDateTime);
                if (DateTime.Compare(dateTime, t2: DateTime.Now) <= 0)
                {
                    continue;
                }

                if (!isOlder && DateTime.Compare(dateTime, closestDateTime) >= 0)
                {
                    closestTime = dateTime.ToString("yyyy-MM-dd");
                    isOlder = true;
                }
                else if (DateTime.Compare(dateTime, closestDateTime) <= 0)
                {
                    closestTime = dateTime.ToString("yyyy-MM-dd");
                }
            }

            DateTime.TryParseExact(closestTime, Pattern, null, DateTimeStyles.None, out var showTime);
            this.cdr.TodayDate = showTime;
            this.lbl_closest.Text = @"最近的ddl是" + closestTime;
        }
    }
}
